<template>
	<view class="doctorAdvice">
		<up-button type="primary" text="扫码执行" @click="scanCodeFn" color='#32c9ca'></up-button>
	</view>
	<up-toast ref="uToastRef"></up-toast>
</template>

<script setup>
	import {
		ref
	} from 'vue'

	import {
		getCmdInfoAPI,
		executeCmdAPI
	} from "../../../api/index.js"
	const uToastRef = ref(null)
	// 执行医嘱
	const executeCmdFn = async (id) => {
		let cmdInfo = await getCmdInfoAPI(id)
		if (cmdInfo.code == 200) {
			cmdInfo.data.cmdState = '已完成'
			cmdInfo.data.patientCmdExecutionRecordList = [{
				cmdState: '已完成'
			}]
			let arr = []
			arr.push(cmdInfo.data)

			// 发请求 执行医嘱
			let executeRes = await executeCmdAPI(arr)
			if (executeRes.code == 200) {
				uToastRef.value.show({
					type: 'success',
					message: '医嘱执行成功',
				})
			} else {
				uToastRef.value.show({
					type: 'success',
					message: '医嘱执行失败,请重试',
				})
			}
		} else {
			uToastRef.value.show({
				type: 'error',
				message: '医嘱详情查询失败,请重试',
			})
		}
	}

	const scanCodeFn = () => {
		// executeCmdFn(120909)
		// return
		uni.scanCode({
			success: (res) => {
				executeCmdFn(JSON.parse(res.result).id)
			},
			fail: (err) => {
				uToastRef.value.show({
					type: 'success',
					message: '扫描失败,请重试',
				})
			}
		});
	}
</script>

<style scoped lang="scss">
	.doctorAdvice {
		padding: 20rpx;

		.execute {}

	}
</style>